package com.neusoft.nepm.service.impl;

import java.util.List;

import com.neusoft.nepm.dto.GmRemarksDto;
import com.neusoft.nepm.dto.GmpageRequestDto;
import com.neusoft.nepm.dto.PageResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.nepm.mapper.GridMemberMapper;
import com.neusoft.nepm.po.GridMember;
import com.neusoft.nepm.service.IGridMemberService;

@Service
public class GridMemberServiceImpl implements IGridMemberService {
	
	@Autowired
	private GridMemberMapper gridMemberMapper;
	
	/********************** nepm工程 ***********************/
	@Override
	public List<GridMember> listGridMemberByProvinceId(GridMember gridMember) {
		QueryWrapper<GridMember> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("province_id", gridMember.getProvinceId());
		queryWrapper.eq("city_id", gridMember.getCityId());
		queryWrapper.eq("state", gridMember.getState());
		return gridMemberMapper.selectList(queryWrapper);
	}
	
	/********************** nepm工程 ***********************/
	
	/********************** nepg工程 ***********************/
	@Override
	public GridMember getGridMemberByCodeByPass(GridMember gridMember) {
		return gridMemberMapper.getGridMemberByCodeByPass(gridMember);
	}

	@Override
	public PageResponseDto<GridMember> listGridMemberPage(GmpageRequestDto request) {
		PageResponseDto<GridMember> response = new PageResponseDto<>();

		//获取总行数
		int totalRow = gridMemberMapper.getGridMemberCount(request);
		response.setTotalRow(totalRow);

		//如果总行数为0，那么直接返回
		if(totalRow==0) {
			return response;
		}

		//计算总页数
		int totalPageNum = 0;
		if(totalRow%request.getMaxPageNum()==0) {
			totalPageNum = totalRow/request.getMaxPageNum();
		}else {
			totalPageNum = totalRow/request.getMaxPageNum()+1;
		}
		response.setTotalPageNum(totalPageNum);

		//计算上一页和下一页
		int pageNum = request.getPageNum();
		response.setPreNum(pageNum);
		response.setNextNum(pageNum);
		if(pageNum>1) {
			response.setPreNum(pageNum-1);
		}
		if(pageNum<totalPageNum) {
			response.setNextNum(pageNum+1);
		}

		//计算开始查询记录数
		request.setBeginNum((pageNum-1)*request.getMaxPageNum());

		//查询业务数据
		List<GridMember> list = gridMemberMapper.listGridMemberPage(request);
		//给返回值填充余下数据
		response.setPageNum(pageNum);
		response.setMaxPageNum(request.getMaxPageNum());
		response.setList(list);

		System.out.println(totalRow);
		System.out.println(list.size());

		for(GridMember g:list){
			System.out.println(g);
		}

		return response;
	}

	@Override
	public String addRemarksById(GmRemarksDto gmRemarksDto) {
		System.out.println(gmRemarksDto.getGmId()+"+"+gmRemarksDto.getRemarks());
		gridMemberMapper.addRemarksById(gmRemarksDto);
		return "num";
	}

	@Override
	public GridMember getGridMemberById(String gmId) {
		return gridMemberMapper.selectById(gmId);
	}

	@Override
	public void updateSupervisor(GridMember gridMember) {
		gridMemberMapper.updateById(gridMember);
	}

	/********************** nepg工程 ***********************/

}
